package com.tianhui.consignortwofei.mvp.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.message.MessageInfo;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;
import org.yczbj.ycrefreshviewlib.swipe.OnSwipeMenuListener;
import org.yczbj.ycrefreshviewlib.swipe.YCSwipeMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2020/9/10
 * @description
 **/
public class MessageListAdapter extends RecyclerArrayAdapter<MessageInfo> {
    private boolean isEdit;


    public MessageListAdapter(Context context) {
        super(context);
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private OnSwipeMenuListener listener;

    public void setOnSwipeMenuListener(OnSwipeMenuListener listener) {
        this.listener = listener;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MessageListViewHolder(parent, R.layout.item_message_list);
    }

    public List<MessageInfo> getSelectedMessage() {
        List<MessageInfo> messageInfoList = new ArrayList<>();
        for (MessageInfo messageInfo : getAllData()) {
            if (messageInfo.isChecked) {
                messageInfoList.add(messageInfo);
            }
        }
        return messageInfoList;
    }


    private class MessageListViewHolder extends BaseViewHolder<MessageInfo> {

        private TextView titleTextView;
        private TextView timeTextView;
        private TextView contentTextView;
        private ImageView imageView;
        private ImageView checkImageView;
        private FrameLayout checkBoxFrameLayout;
        private FrameLayout deleteFrameLayout;
        private YCSwipeMenu swipeMenu;

        public MessageListViewHolder(ViewGroup parent, int res) {
            super(parent, res);
            titleTextView = getView(R.id.item_message_list_titleTextView);
            timeTextView = getView(R.id.item_message_list_timeTextView);
            contentTextView = getView(R.id.item_message_list_contentTextView);
            imageView = getView(R.id.item_message_list_imageView);
            checkImageView = getView(R.id.item_message_list_checkImageView);
            checkBoxFrameLayout = getView(R.id.item_message_list_checkBoxFrameLayout);
            deleteFrameLayout = getView(R.id.item_message_list_deleteFrameLayout);
            swipeMenu = getView(R.id.item_messageList_swipeMenu);

            addOnClickListener(R.id.item_message_list_itemLinearLayout);
        }

        @Override
        public void setData(MessageInfo data) {
            super.setData(data);
            int position = MessageListAdapter.this.getPosition(data);
            checkBoxFrameLayout.setVisibility(isEdit ? View.VISIBLE : View.GONE);
            swipeMenu.setSwipeEnable(!isEdit);

            titleTextView.setText(data.title);
            timeTextView.setText(data.createTime);
            contentTextView.setText(data.content);
            if (data.remindFlag == 1) {
                imageView.setBackgroundResource(R.mipmap.icon_message_read);
            } else {
                imageView.setBackgroundResource(R.mipmap.icon_message_unread);
            }

            showCheckStatus(data.isChecked);

            deleteFrameLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.toDelete(getDataPosition());
                    }
                }
            });

            checkBoxFrameLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    data.isChecked = !data.isChecked;
                    showCheckStatus(data.isChecked);
                    notifyItemChanged(position);
                }
            });

        }
        private void showCheckStatus(boolean isChecked) {
            if (isChecked){
                checkImageView.setBackgroundResource(R.mipmap.icon_checked);
            }else {
                checkImageView.setBackgroundResource(R.mipmap.icon_unchecked);
            }
        }
    }



}
